/*
 * Copyright 2007,2008 Anton Dignoes, Daniel Rampanelli, Martin Palma
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.webduff.util;

import java.util.Hashtable;
import java.util.Map;

import com.googlecode.webduff.io.URI;


public class HtmlUtilities {

	private static final String DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
	
	private static final String POWERED = "<address>powered by WebDuff</address>\n";
	
	private static Map<String, String> registeredMimeTypes = new Hashtable<String, String>();
	
	static {
		registeredMimeTypes.put("*", "/images/file.png");
		registeredMimeTypes.put("special/folder", "/images/folder.png");
		registeredMimeTypes.put("application/pdf", "/images/office-document.png");
		registeredMimeTypes.put("application/x-gzip", "/images/package.png");
		registeredMimeTypes.put("application/zip", "/images/package.png");
		registeredMimeTypes.put("text/plain", "/images/text.png");
		registeredMimeTypes.put("audio/x-wav", "/images/audio.png");
		registeredMimeTypes.put("audio/x-mpeg", "/images/audio.png");
		registeredMimeTypes.put("image/png", "/images/image.png");
		registeredMimeTypes.put("image/jpeg", "/images/image.png");
		registeredMimeTypes.put("image/gif", "/images/image.png");
		registeredMimeTypes.put("image/x-photoshop", "/images/image.png");
		registeredMimeTypes.put("video/quicktime", "/images/video.png");
		registeredMimeTypes.put("video/x-msvideo", "/images/video.png");
		registeredMimeTypes.put("application/vnd.oasis.opendocument.text", "/images/office-document.png");
		registeredMimeTypes.put("application/vnd.oasis.opendocument.spreadsheet", "/images/office-spreadsheet.png");
		registeredMimeTypes.put("application/vnd.oasis.opendocument.presentation", "/images/office-presentation.png");
		registeredMimeTypes.put("application/vnd.ms-word", "/images/office-document.png");
		registeredMimeTypes.put("application/vnd.ms-excel", "/images/office-spreadsheet.png");
		registeredMimeTypes.put("application/vnd.ms-powerpoint", "/images/office-presentation.png");
	}
	
	public static String getHeader(String title) {
		return DOCTYPE + "\n" + "<html>\n<head>\n<title>" + title + "</title>\n</head>\n<body>\n";
	}
	
	public static String getBottom() {
		return "<hr />" + POWERED + "</body>\n</html>";
	}
	
	public static String getHref(String name, String url) {
		return "<a href=\"" + url + "\">" + name + "</a>";
	}
	
	public static String getHref(String name, URI url) {
		return getHref(name, url.getPath());
	}
	
	public static String getImage(String url) {
		return "<img src=\"" + url + "\" >";
	}

	public static String getImage(URI url) {
		return getImage(url.getPath());
	}
	
	public static String getImageForMimeType(String mimeType) {
		if(mimeType != null && registeredMimeTypes.containsKey(mimeType.trim())) {
			return registeredMimeTypes.get(mimeType.trim());
		}
		return registeredMimeTypes.get("*");
	}
	
}